package com.example.myapplication.model.rx;

import android.accounts.NetworkErrorException;
import android.net.ParseException;

import com.example.myapplication.R;
import com.example.myapplication.app.App;
import com.example.myapplication.model.bean.HttpResult;
import com.example.myapplication.utils.ToastUtils;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Created by yin97 on 2017/5/1.
 */

public abstract class BaseObserver<T>  implements Observer<HttpResult<T>> {

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(HttpResult<T> value) {

//        if (value.getStatus()==1) {
            List<T> t = value.getData();
            _onSuccess(t);
//        } else {
//            onHandleError(value.getMsg());
//        }
    }

    @Override
    public void onError(Throwable e) {
        //请求异常提示
        String toast = "";
        String reason = "";
        if (e instanceof NetworkErrorException){

        }else if (e instanceof UnknownHostException)
            toast =App.getInstance().getString(R.string.network_connection_faile);
        else if (e instanceof SocketTimeoutException)
            toast = App.getInstance().getString(R.string.network_connection_time_out);
        else if (e instanceof ConnectException)
            toast = App.getInstance().getString(R.string.network_connection_time_out);
        else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int responseCode = httpException.code();
            if (responseCode >= 400 && responseCode <= 417) {
                toast = App.getInstance().getString(R.string.common_url_error);
            } else if (responseCode >= 500 && responseCode <= 505) {
                toast = App.getInstance().getString(R.string.network_connection_busy);
            } else {
                toast = App.getInstance().getString(R.string.network_connection_exception);
            }
        } else if (e instanceof ResultException) {
            ResultException resultException = (ResultException) e;
            int resultCode = resultException.getErrCode();
            if (resultCode == -1) {
//                GwtKeyApp.getInstance().doReLogin();
            } else if (resultCode <= 0) {
                reason = resultException.getMessage();
            } else {
                //正常不会进到这里
                toast = App.getInstance().getString(R.string.common_data_exception);
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException
                || e instanceof NullPointerException)
            toast = App.getInstance().getString(R.string.common_data_exception);
        else
            toast = App.getInstance().getString(R.string.common_unknown_error);

        ToastUtils.showToast(toast);
        _onError();
    }

    @Override
    public void onComplete() {

    }

    protected abstract void _onSuccess(List<T> t);
    protected abstract void _onError();
    protected abstract void onHandleError(String msg);
}
